package com.his.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.his.common.PageParam;
import com.his.common.Rsp;
import com.his.entity.MedicinalCategroy;
import com.his.mapper.MedicinalCategroyMapper;
import com.his.service.MedicinalCategroyService;
import com.his.utils.MyUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (MedicinalCategroy)表服务实现类
 *
 * @author makejava
 * @since 2020-10-23 18:12:56
 */
@Service("medicinalCategroyService")
public class MedicinalCategroyServiceImpl implements MedicinalCategroyService {
    @Resource
    private MedicinalCategroyMapper medicinalCategroyMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public MedicinalCategroy queryById(Integer id) {
        return this.medicinalCategroyMapper.queryById(id);
    }

    /**
     * 查询所有通过实体类属性
     *
     * @param medicinalCategroy 实例对象
     * @return 查询的结果
     */
    @Override
    public List<MedicinalCategroy> queryAll(MedicinalCategroy medicinalCategroy) {
        return this.medicinalCategroyMapper.queryAll(medicinalCategroy);
    }

    /**
     * 查询所有通过实体类属性
     *
     * @param pageParam
     * @return
     */
    public PageInfo<MedicinalCategroy> queryAll(PageParam<MedicinalCategroy> pageParam) {

        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            return new PageInfo<MedicinalCategroy>(medicinalCategroyMapper.superSearch(pageParam.getSuperSearchKeyWord()));
        }
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        //排序
        for (int i = 0; i < pageParam.getOrderParams().length; i++) {
            PageHelper.orderBy(pageParam.getOrderParams()[i]);
        }
        return new PageInfo<MedicinalCategroy>(medicinalCategroyMapper.queryAllByModel(pageParam.getModel()));
    }

    /**
     * 新增数据
     *
     * @param medicinalCategroy 实例对象
     * @return 实例对象
     */
    @Override
    public Rsp insert(String token, MedicinalCategroy medicinalCategroy) {
        //获取用户Id
        Integer userId = null;
        try {
            userId = MyUtil.getUserByToken(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        medicinalCategroy.setCreateId(userId);//创建则ID
        medicinalCategroy.setCreateTime(new Date());//创建时间
        medicinalCategroy.setIsDelete(0);//默认不删除
        //判断类别名称是否重复
        Integer queryByName = medicinalCategroyMapper.queryByName(medicinalCategroy.getCategroyName());
        if (queryByName > 0) {
            return Rsp.fail().msg("类别名重复");
        }
        //没重复则返回添加的实体
        int insert = this.medicinalCategroyMapper.insert(medicinalCategroy);
        return insert > 0 ? Rsp.success(medicinalCategroy).msg("添加成功")
                : Rsp.fail().msg("添加失败");
    }

    /**
     * 修改数据
     *
     * @param medicinalCategroy 实例对象
     * @return 实例对象
     */
    @Override
    public Rsp update(String token, MedicinalCategroy medicinalCategroy) {
        //判断类别名称是否重复
        Integer queryByName = medicinalCategroyMapper.queryByName(medicinalCategroy.getCategroyName());
        if (queryByName > 0) {
            return Rsp.fail().msg("类别名称重复");
        }
        //获取修改id
        Integer userByToken = null;
        try {
            userByToken = MyUtil.getUserByToken(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        medicinalCategroy.setUpdataId(userByToken);
        medicinalCategroy.setUpdataTime(new Date());//修改时间
        int update = this.medicinalCategroyMapper.update(medicinalCategroy);
        return update > 0 ? Rsp.success(medicinalCategroy).msg("修改成功") : Rsp.fail().msg("修改失败");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.medicinalCategroyMapper.deleteById(id) > 0;
    }

    /**
     * 通过主键逻辑删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public Rsp deleteByIdLogic(Integer id) {
        return this.medicinalCategroyMapper.deleteByIdLogic(id) > 0 ? Rsp.success("删除成功" + id) : Rsp.fail().msg("删除失败");
    }

    /**
     * 查询所有类别列表
     *
     * @return
     */
    @Override
    public Rsp searchCategroyList() {
        return Rsp.success(medicinalCategroyMapper.searchCategroyList());
    }

}
